﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.BuisnessComponents;
using System.Collections.ObjectModel;

namespace MovieRental.EnhancedEntities
{
    public class LoanCart : BaseEnhancedEntity<LoanCartInfo>
    {
        private ObservableCollection<Loan> _loans = new ObservableCollection<Loan>();

        public LoanCart(LoanCartInfo entity)
            : base(entity)
        {
        }

        public LoanCart()
            : base(new LoanCartInfo())
        {
        }

        protected override void EntityChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            OnPropertyChanged("CustomerName");
            OnPropertyChanged("Loans");
            OnPropertyChanged("InitialCost");
            OnPropertyChanged("PersonalDiscountValue");
            OnPropertyChanged("CartDiscountValue");
            OnPropertyChanged("TotalCost");
            OnPropertyChanged("Quantity");
        }

        public string CustomerName
        {
            get
            {
                var customerComponent = ComponentFactory.Instance.GetCustomerComponent();
                CustomerInfo customer = customerComponent.Get(Entity.CustomerId);
                return (customer == null) ? null : string.Format("{0} {1}", customer.FirstName, customer.LastName);
            }
        }

        public int NotReturnedLoans
        {
            get { return Loans.Count(); }
        }

        public double PersonalDiscountValue
        {
            get { return ComponentFactory.Instance.GetPersonalDiscountComponent().GetAvailablePersonalDiscountValue(Entity.CustomerId); }
        }

        public double CartDiscountValue
        {
            get { return ComponentFactory.Instance.GetCartDiscountComponent().GetAvailableCartDiscountValue(Entity.Loans.Count()); }
        }

        public double InitialCost
        {
            get { return ComponentFactory.Instance.GetLoanCartComponent().GetInitialCostPerDay(Entity); }
        }

        public double TotalCost
        {
            get { return ComponentFactory.Instance.GetLoanCartComponent().GetTotalCostPerDay(Entity); }
        }


        public ObservableCollection<Loan> Loans
        {
            get
            {
                _loans.Clear();
                foreach (var loan in Entity.Loans.Where(x => x.ReturnDate == DateTime.MinValue))
                    _loans.Add(new Loan(loan));
                return _loans;
            }
        }

    }
}
